<?php
class user extends table
{
	//protected $_name	= 'my_user';
	//protected $fields	= array('uid','username','password','gid');
	//protected $_primary = 'uid';
	protected $_name	= 'my_member';
	protected $fields	= array('id','wid','email','username','password','province','city','name','tel','fax','login_time','register_time');
	protected $_primary = 'id';

	public static function needLogin()
	{
		require_once ROOT . 'libs/Zend/Auth.php';
		require_once ROOT . 'libs/Zend/Auth/Storage/Session.php';

		$auth			= Zend_Auth::getInstance();
		$auth->setStorage(new Zend_Auth_Storage_Session('user'));

		if (isPost() && isset($_POST['action']) && $_POST['action'] == 'login')
		{
			self::login(array('username'=>$_POST['username'], 'password'=>$_POST['password']));
		}
		else if (!$auth->hasIdentity())
		{
			require_once (ROOT . '/template/login.php');
			die();
		}
		session_write_close();
	}
	
	public function login($arg)
	{
		require_once ROOT . 'libs/Zend/Auth.php';
		require_once ROOT . 'libs/Zend/Auth/Adapter/DbTable.php';
		require_once ROOT . 'libs/Zend/Auth/Storage/Session.php';

		$db				= parent::db();
		$sucess			= false;
		$auth			= Zend_Auth::getInstance();
		$authAdapter	= new Zend_Auth_Adapter_DbTable($db,'my_member','username','password');
		
		$authAdapter->setIdentity($arg['username'])->setCredential(md5($arg['password']));
		$auth->setStorage(new Zend_Auth_Storage_Session('user'));
		
		$result			= $auth->authenticate($authAdapter);

		if($result->isValid())
		{
			$storage = $auth->getStorage();
			$storage->write($authAdapter->getResultRowObject(null, 'password'));
			
			if($auth->getStorage()->read()->role)
			{
				$sucess	= true;
			}
		}
		if (!$sucess) 
		{	
			$message = 'A record with the supplied identity could not be found or Supplied credential is invalid <br />用户名或密码无效';
			require_once (ROOT . '/template/login.php');
			die();
		}
	}
	
	public static function logout()
	{
		require_once ROOT . 'libs/Zend/Auth/Storage/Session.php';
		require_once ROOT. 'libs/Zend/Auth.php';
		
		session_start();
		$auth	= Zend_Auth::getInstance();
		$auth->setStorage(new Zend_Auth_Storage_Session('user'));
		$auth->clearIdentity();
		#Zend_Session::destroy(false,true);
	}

    public static function getInfo()
	{
		/*require_once ROOT . 'libs/Zend/Auth/Storage/Session.php';
		require_once ROOT . 'libs/Zend/Auth.php';
		$auth	= Zend_Auth::getInstance();
		$auth->setStorage(new Zend_Auth_Storage_Session('user'));
		$user	= (array)$auth->getIdentity();
        if(!empty($user)) $user['gid'] = explode(',',$user['gid']);
		return $user;
		*/
		require_once ROOT . 'libs/Zend/Auth.php';
		
		$db				= parent::db();
		
		$auth			= Zend_Auth::getInstance();
		$auth->setStorage(new Zend_Auth_Storage_Session('user'));
		$user			= (array)$auth->getIdentity();
		$user			= base::init('member')->fetchRow("id={$user['id']}");
		$sql			= "select * from my_member_group as m,my_member_group_control as g where g.member_group_id=m.id and g.member_id={$user['id']}";
		$user['group']	= $db->fetchRow($sql);
		$user['score']	= base::init('score')->fetchRow(array('id'=>$user['id']));

		return $user;
	}

    public function getRow($where=null)
    {
        $data   = parent::getRow($where);
        $data['gid'] = explode(',',$data['gid']);
        return $data;
    }

}
